﻿using System;
using System.Windows.Forms;
using 售后客户管理.Common;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms.InternalSetting
{
    public partial class FmDictionary : Form
    {
        public FmDictionary()
        {
            InitializeComponent();
        }
        private SqlConnectionHelper SqlConnectionHelper = new SqlConnectionHelper(Program.configs,ConfigCode.Main);
        private void FmDictionary_Load(object sender, EventArgs e)
        {
            this.btnSave.Paint += DrawingImage.RoundButton_Paint;
            this.btnCancel.Paint += DrawingImage.RoundButton_Paint;
            this.plSplitLine.Height = 1;
            ObjectToApp(new Dictionary());
            this.dgvDictionary.BackgroundColor = MyColor.NewColor_Lavender;
            this.dgvDictionary.DoubleBuffered(true);
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            Dictionary dictionary = AppToObject();
            if (dictionary.Colname == "")
            {
                return;
            }
            if (SqlClassLibrary.SqlGetSet.SetDictionary(dictionary, SqlConnectionHelper.sqlConnection) > 0)
            {
                if (MessageBox.Show("保存成功！是否清除界面信息？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    ObjectToApp(new Dictionary());
                FmSelectMenu_Selected(AppToObject());
                this.fbcColname.Focus();
                return;
            }
            MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        private void FbcColname_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                ActSelectColumn();
            }
        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            ObjectToApp(new Dictionary());
        }
        private void FbcColname_DoubleClick(object sender, EventArgs e)
        {
            ActSelectColumn();
        }
        private void DgvDictionary_DoubleClick(object sender, EventArgs e)
        {
            Dictionary dictionary = new Dictionary();
            dictionary = DrawingImage.DataGridViewSelectedRowToObject(this.dgvDictionary, dictionary);
            dictionary.OldDict = dictionary.Dict;
            ObjectToApp(dictionary);
        }


        private Dictionary AppToObject()
        {
            Dictionary dictionary = new Dictionary();
            dictionary.Beactive = cbBeactive.Checked ? "Y" : "N";
            dictionary.Beizhu = this.fbcBeizhu.Text;
            dictionary.Chnname = this.fbcChnname.Text;
            dictionary.Colname = this.fbcColname.Text;
            dictionary.Dict = this.fbcDict.Text;
            dictionary.OldDict = this.tbOldDict.Text;
            dictionary.ViewSort = this.fbcViewSort.Text;

            return dictionary;
        }
        private void ObjectToApp(Dictionary dictionary)
        {
            this.cbBeactive.Checked = dictionary.Beactive == "Y" ? true : false;
            this.fbcBeizhu.Text = dictionary.Beizhu;
            this.fbcChnname.Text = dictionary.Chnname;
            this.fbcColname.Text = dictionary.Colname;
            this.fbcDict.Text = dictionary.Dict;
            this.tbOldDict.Text = dictionary.OldDict;
            this.fbcViewSort.Text = dictionary.ViewSort;


        }
        private void ActSelectColumn()
        {
            FmSelectMenu fmSelectMenu = new FmSelectMenu("dictionary", new Dictionary() { Colname = this.fbcColname.Text, Chnname = this.fbcColname.Text, }, SqlConnectionHelper.sqlConnection);
            fmSelectMenu.Selected += FmSelectMenu_Selected;
            fmSelectMenu.ShowDialog();
        }
        private void FmSelectMenu_Selected(object selected)
        {
            Dictionary dictionary = (Dictionary)selected;
            ObjectToApp(dictionary);
            this.dgvDictionary.DataSource = SqlClassLibrary.SqlGetSet.GetDictionaryDT(dictionary, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dgvDictionary, SqlConnectionHelper.sqlConnection);
        }

        private void FmDictionary_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                BtnSave_Click(this.btnSave, new EventArgs());
            }
        }
    }
}
